package org.come.mouslisten;

import come.tool.Fighting.FightingMixDeal;
import org.come.Frame.MountJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.ZhuJpanel;
import org.come.Jpanel.mountJPanel;
import org.come.entity.Mount;
import org.come.entity.RoleSummoning;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.*;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.util.List;

public class MountMouslisten implements MouseListener, MouseMotionListener
{
    private JList<String> listpet;
    private mountJPanel oum;

    public MountMouslisten(final JList<String> listpet, final mountJPanel oum) {
        this.listpet = listpet;
        this.oum = oum;
    }

    @Override
    public void mouseClicked(final MouseEvent e) {
        if (e.isMetaDown()) {
            FormsManagement.HideForm(6);
        }
    }

    @Override
    public void mousePressed(final MouseEvent e) {
       // FormsManagement.showForm(289);
        final List<RoleSummoning> pets = UserMessUntil.getPetListTable();
        if (UserMessUntil.getPetListTable().size() != 0) {
            if (this.listpet.getSelectedIndex() != -1) {
                UserMessUntil.setChosePetMes((RoleSummoning)UserMessUntil.getPetListTable().get(this.listpet.getSelectedIndex()));
            }
            if (e.getButton ()== MouseEvent.BUTTON3){
                UserMessUntil.setChosePetMes((RoleSummoning)UserMessUntil.getPetListTable().get( mountJPanel.idx));
                RoleSummoning roleSummoning=UserMessUntil.getChosePetMes();
                if (roleSummoning!=null&& FightingMixDeal.State == 0){
                    this.controlPet(roleSummoning);
                }
                return;
            }
            if (e.getButton() == 1 && UserMessUntil.getChosePetMes() != null) {
                if (e.isShiftDown()) {
//                    try {
//                        final JTextField SendMes = ZhuFrame.getZhuJpanel().getSendMes();
////                        ((RichDocument)SendMes.getDocument()).insertRich(SendMes.getCaretPosition(), 3, UserMessUntil.getChosePetMes().getSid(), "[" + UserMessUntil.getChosePetMes().getSummoningnameg() + "]", "G", null);
//                    }
//                    catch (BadLocationException e2) {
//                        e2.printStackTrace();
//                    }
                }
                if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE10)) {
                    MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
                    if (UserMessUntil.getChosePetMes().getPetlock() == 1) {
                        ZhuFrame.getZhuJpanel().addPrompt("该召唤兽已加锁");
                    }
                    else {
                        UserMessUntil.getChosePetMes().setPetlock(1);
                        try {
                            SendRoleAndRolesummingUntil.sendRoleSumming(UserMessUntil.getChosePetMes());
                        }
                        catch (Exception e3) {
                            e3.printStackTrace();
                        }
                        ZhuFrame.getZhuJpanel().addPrompt("加锁成功");
                    }
                }
                if (MessagrFlagUntil.ImgFlagImg.equals(MessagrFlagUntil.MOUSE11) && UserMessUntil.getChosePetMes().getPetlock() == 1) {
                    MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);
                    UserMessUntil.getChosePetMes().setPetlock(0);
                    try {
                        SendRoleAndRolesummingUntil.sendRoleSumming(UserMessUntil.getChosePetMes());
                    }
                    catch (Exception e3) {
                        e3.printStackTrace();
                    }
                    ZhuFrame.getZhuJpanel().addPrompt("解锁成功");
                }
            }
            if (this.listpet.getSelectedIndex() == -1) {
                return;
            }
            else {

                if (FormsManagement.getframe(18).isVisible()) {
                    ChosePetSkillsMouslisten.refreshPetSkills();
                }
                if (FormsManagement.getframe(601).isVisible()) {
                    ChosePetLxMouslisten.refreshPetSkills();
                }
                if (FormsManagement.getframe(602).isVisible()) {
                    FormsManagement.HideForm(602);
                }
            }
        }
    }

    @Override
    public void mouseReleased(final MouseEvent e) {
//        PetSkillsJpanel.getEffect().setBounds(1000, 1000, 50, 50);
    }

    @Override
    public void mouseEntered(final MouseEvent e) {
    }

    @Override
    public void mouseExited(final MouseEvent e) {
        ZhuFrame.getZhuJpanel().pettext();
        mountJPanel.idx = -1;
    }

    @Override
    public void mouseDragged(final MouseEvent e) {
    }

    @Override
    public void mouseMoved(final MouseEvent e) {
        if (e.getY() / 24 < this.listpet.getModel().getSize()) {
            mountJPanel.idx = this.listpet.locationToIndex(e.getPoint());
        }
    }
    public void controlPet(RoleSummoning pet) {
        if (MountJframe.getMountjframe().getMountjpanel().getListmount().getSelectedValue() == null) {
            ZhuFrame.getZhuJpanel().addPrompt2("请选择坐骑！");
            return;
        }
        else {
            if (false) {
                ZhuFrame.getZhuJpanel().addPrompt2("请选择你要管制的召唤兽！");
                return;
            }
            else {
                final int index1 = MountJframe.getMountjframe().getMountjpanel().getListmount().getSelectedIndex();
                //  final int index2 = mountJPanel.getListpet().getSelectedIndex();
                //  pet = (RoleSummoning)UserMessUntil.getPetListTable().get(index2);
                final Mount mount = (Mount) ZhuJpanel.getListMount().get(index1);
                final Mount mount2 = ZhuJpanel.getPetMount(pet.getSid());
                if (mount2 != null && mount != mount2) {
                    ZhuFrame.getZhuJpanel().addPrompt2("该召唤兽已被其他坐骑管制");
                    return;
                }
                else {
                    int type = 0;
                    if (mount.getSid() != null && pet.getSid().compareTo(mount.getSid()) == 0) {
                        //  type = -1;
                        // mount.setSid(null);
                        ZhuFrame.getZhuJpanel().addPrompt2("请点击召唤兽头像解除管制");
                        return;

                    }
                    else {
                        if (mount.getOthrersid() != null && pet.getSid().compareTo(mount.getOthrersid()) == 0) {
                            //   type = -2;
                            //  mount.setOthrersid(null);
                            ZhuFrame.getZhuJpanel().addPrompt2("请点击召唤兽头像解除管制");
                            return;
                        }
                        else {
                            if (mount.getSid3() != null && pet.getSid().compareTo(mount.getSid3()) == 0) {
                                //  type = -3;
                                //  mount.setSid3(null);
                                ZhuFrame.getZhuJpanel().addPrompt2("请点击召唤兽头像解除管制");
                                return;
                            }
                            else {
                                if (mount.getSid() == null) {
                                    mount.setSid(pet.getSid());
                                    String path = "img/newHead/p" + pet.getSummoningskin() + ".png";
                                    // final ImageIcon icon1 = CutButtonImage.getImage("img/xy2uiimg/1126_png.xy2uiimg.shead.png", -1, -1);
                                    final File file = new File(path);
                                    if (file.exists()) {
                                        final ImageIcon icon2 = CutButtonImage.size(path, 45, 45);
                                        oum.getPetlist()[0].setIcon((Icon)icon2);
                                        oum.getP().put(0,pet);
                                    }
                                    else {
                                        final ImageIcon icon2 = CutButtonImage.getImage("img/xy2uiimg/101_png.xy2uiimg.pet_def.png", -1, -1);
                                        oum.getPetlist()[0].setIcon((Icon)icon2);
                                        oum.getP().put(0,pet);
                                    }
                                    // oum.getPetlist()[0].setIcon();
                                    type = 1;
                                }
                                else {
                                    if (mount.getOthrersid() == null) {
                                        mount.setOthrersid(pet.getSid());
                                        String path = "img/newHead/p" + pet.getSummoningskin() + ".png";
                                        // final ImageIcon icon1 = CutButtonImage.getImage("img/xy2uiimg/1126_png.xy2uiimg.shead.png", -1, -1);
                                        final File file = new File(path);
                                        if (file.exists()) {
                                            final ImageIcon icon2 = CutButtonImage.size(path, 45, 45);
                                            oum.getPetlist()[1].setIcon((Icon)icon2);
                                            oum.getP().put(1,pet);
                                        }
                                        else {
                                            final ImageIcon icon2 = CutButtonImage.getImage("img/xy2uiimg/101_png.xy2uiimg.pet_def.png", -1, -1);
                                            oum.getPetlist()[1].setIcon((Icon)icon2);
                                            oum.getP().put(1,pet);
                                        }
                                        type = 2;
                                    }
                                    else {
                                        if (mount.getMountlvl() > 100 && mount.getSid3() == null) {
                                            mount.setSid3(pet.getSid());
                                            String path = "img/newHead/p" + pet.getSummoningskin() + ".png";
                                            // final ImageIcon icon1 = CutButtonImage.getImage("img/xy2uiimg/1126_png.xy2uiimg.shead.png", -1, -1);
                                            final File file = new File(path);
                                            if (file.exists()) {
                                                final ImageIcon icon2 = CutButtonImage.size(path, 45, 45);
                                                oum.getPetlist()[2].setIcon((Icon)icon2);
                                                oum.getP().put(2,pet);
                                            }
                                            else {
                                                final ImageIcon icon2 = CutButtonImage.getImage("img/xy2uiimg/101_png.xy2uiimg.pet_def.png", -1, -1);
                                                oum.getPetlist()[2].setIcon((Icon)icon2);
                                                oum.getP().put(2,pet);
                                            }
                                            type = 3;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (type == 0) {
                        ZhuFrame.getZhuJpanel().addPrompt2("目前坐骑管制上限!");
                        return;
                    }
                    else {
                        if (type < 0) {
                            ChangeMouseSymbolMouslisten.clearPropertie(pet);
                        }
                        else {
                            ChangeMouseSymbolMouslisten.addProperties(pet, mount);
                        }
                        final String sendmes = Agreement.getAgreement().changeMountValue(GsonUtil.getGsonUtil().getgson().toJson(mount));
                        SendMessageUntil.toServer(sendmes);
                        return;
                    }
                }
            }
        }
    }
}
